/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import model.Bunke;
import model.Deck;
import control.Spil1;
import model.Spiller;
import model.Suit;
import utility.ImageToolbox;

/**
 *
 * @author Jeppe
 */
public class MainGui extends javax.swing.JFrame {

    private ImageToolbox imgTB;
    private PlayersHand playersHand;
    private Spil1 spil;
    private NytSpilDialog nytSpilDialog;
    private KulørVælgerDialog kulørVælgerDialog;
    private IndstillingerDialog indstillingerDialog;
    private final boolean debugMode = false;

    /**
     * Creates new form Olsen
     */
    public MainGui() {
        initComponents();
        imgTB = new ImageToolbox();

        setSize(1024, 710);
        if (debugMode) {
            System.out.println("Kortstørrelse: (" + deckBilledeLabel.getSize().width + "," + deckBilledeLabel.getSize().height + ")");
        }

        spil = new Spil1("Spil", this);

        //Et nyt spil
        Spiller p1 = new Spiller("Jeppe");
        spil.addSpiller(p1);
        spil.addSpiller(new Spiller("CPU"));
        //Spillers hånd til visning.
        playersHand = new PlayersHand(this, p1);
        makePanels();        
        spil.nytSpil();

        //Viser bunkensØverstekort
        bunkeOeversteKortLabel = new JLabel() {
            public void paint(Graphics g) {
                super.paint(g);
                imgTB.drawImage(g, spil.getBunke().getØversteKort().getImageIcon(), 0, 0, 146, 205);
            }
        };
        playersHand.showCardsInPlayersHand();
        deckPanel.add(bunkeOeversteKortLabel);
        bunkeOeversteKortLabel.setVisible(true);
        bunkeOeversteKortLabel.setBounds(172, 2, 146, 205);

        //TEST
    }

    private void makePanels() {
        nytSpilDialog = new NytSpilDialog(this, true);
        kulørVælgerDialog = new KulørVælgerDialog(this, true);
        indstillingerDialog = new IndstillingerDialog(this, true);
        indstillingerDialog.setBounds(250, 250, indstillingerDialog.getSize().width+2, indstillingerDialog.getSize().height+2);
        playersHand.setBounds(0, 0, playerHandPanel.getWidth(), playerHandPanel.getHeight());
        playersHand.setVisible(true);
        playerHandPanel.add(playersHand);

        playerHandPanel.repaint();
    }

    public void updateShowHand() {
        //Viser spillerens kort
        playersHand.showCardsInPlayersHand();
        playersHand.repaint();
        bunkeOeversteKortLabel.repaint();
        if (debugMode) {
        System.out.println("Vi opdaterer");
        }
    }

    public Spil1 getSpil() {
        return spil;
    }

    public void visKulørVælger() {
        kulørVælgerDialog.setBounds(400, 400, kulørVælgerDialog.getSize().width, kulørVælgerDialog.getSize().height);
        kulørVælgerDialog.setVisible(true);
        
    }

    public void stopVisningAfKulørVælger(Suit suit) {
        kulørVælgerDialog.setVisible(false);
        spil.setCurSuit(suit);
        if (debugMode) {
            System.out.println("Den valgte kulør er " + suit.toString());
        }
    }
    
    public void setDeckIcon(ImageIcon imgIcon){
        deckBilledeLabel.setIcon(imgIcon);
    }
    
    public void setBackGround(ImageIcon imgIcon){
        baggrund.setIcon(imgIcon);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        playerHandPanel = new javax.swing.JPanel();
        deckPanel = new javax.swing.JPanel();
        deckBilledeLabel = new javax.swing.JLabel(){
            public void paint (Graphics g){
                super.paint(g);
                imgTB.drawImage(g, (ImageIcon)deckBilledeLabel.getIcon(), 0, 0, deckBilledeLabel.getSize().width, deckBilledeLabel.getSize().height);
            }
        }
        ;
        bunkeOeversteKortLabel = new javax.swing.JLabel();
        baggrund = new javax.swing.JLabel(){
            public void paint (Graphics g){
                super.paint(g);
                imgTB.drawImage(g, (ImageIcon)baggrund.getIcon(), 0, 0, baggrund.getSize().width, baggrund.getSize().height);
            }
        }
        ;
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        nytSpilMenuKnap = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel3.setOpaque(false);
        jPanel3.setLayout(null);

        playerHandPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        playerHandPanel.setOpaque(false);

        javax.swing.GroupLayout playerHandPanelLayout = new javax.swing.GroupLayout(playerHandPanel);
        playerHandPanel.setLayout(playerHandPanelLayout);
        playerHandPanelLayout.setHorizontalGroup(
            playerHandPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 776, Short.MAX_VALUE)
        );
        playerHandPanelLayout.setVerticalGroup(
            playerHandPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 286, Short.MAX_VALUE)
        );

        jPanel3.add(playerHandPanel);
        playerHandPanel.setBounds(130, 350, 780, 290);

        deckPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        deckPanel.setOpaque(false);
        deckPanel.setLayout(null);

        deckBilledeLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/DeckKort.PNG"))); // NOI18N
        deckBilledeLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                deckBilledeLabelMouseClicked(evt);
            }
        });
        deckPanel.add(deckBilledeLabel);
        deckBilledeLabel.setBounds(2, 2, 146, 205);

        bunkeOeversteKortLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                bunkeOeversteKortLabelMouseClicked(evt);
            }
        });
        deckPanel.add(bunkeOeversteKortLabel);
        bunkeOeversteKortLabel.setBounds(172, 2, 146, 205);

        jPanel3.add(deckPanel);
        deckPanel.setBounds(343, 95, 320, 210);

        baggrund.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/baggrund.jpg"))); // NOI18N
        jPanel3.add(baggrund);
        baggrund.setBounds(0, 0, 1020, 660);

        jMenu1.setText("Spil");

        nytSpilMenuKnap.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F2, 0));
        nytSpilMenuKnap.setText("Nyt Spil");
        nytSpilMenuKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nytSpilMenuKnapActionPerformed(evt);
            }
        });
        jMenu1.add(nytSpilMenuKnap);
        jMenu1.add(jSeparator1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F10, 0));
        jMenuItem1.setText("Indstillinger");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem1);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 1019, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 659, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void deckBilledeLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_deckBilledeLabelMouseClicked
        if (debugMode) {
        System.out.println("En spiller vil trække et kort op.");
        }
        spil.trækKortOp(spil.getAktivSpiller(), 1);
        updateShowHand();
    }//GEN-LAST:event_deckBilledeLabelMouseClicked

    private void bunkeOeversteKortLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bunkeOeversteKortLabelMouseClicked
        if (debugMode) 
        System.out.println("Du har klikket på bunkens øverste kort : " + spil.getBunke().getØversteKort().toString());
    }//GEN-LAST:event_bunkeOeversteKortLabelMouseClicked

    private void nytSpilMenuKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nytSpilMenuKnapActionPerformed
        nytSpilDialog.setVisible(true);
    }//GEN-LAST:event_nytSpilMenuKnapActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        indstillingerDialog.setVisible(true);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainGui().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel baggrund;
    private javax.swing.JLabel bunkeOeversteKortLabel;
    private javax.swing.JLabel deckBilledeLabel;
    private javax.swing.JPanel deckPanel;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JMenuItem nytSpilMenuKnap;
    private javax.swing.JPanel playerHandPanel;
    // End of variables declaration//GEN-END:variables
}
